using System;
using System.Drawing;
using System.Windows.Forms;

namespace Glpt.Common
{
    /// <summary> 
    ///带关闭按钮TabControl控件
    /// </summary>
    public class GJTabControl : TabControl
    {
        private int iconWidth = 7;
        private int iconHeight = 7;
        /// <summary>
        /// 关闭后事件
        /// </summary>
        public event System.EventHandler PageClosed;
        /// <summary>
        /// 关闭后方法
        /// </summary>
        private void GJTabControl_PageClosed(object sender, EventArgs e)
        {
        }
        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        public GJTabControl()
            : base()
        {
            this.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.Padding = new Point(this.Padding.Y + iconWidth, this.Padding.Y);
            PageClosed += new EventHandler(GJTabControl_PageClosed);
        }
        /// <summary>
        /// OnDrawItem
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            TabPage page = this.TabPages[e.Index];
            Graphics g = e.Graphics;
            Rectangle r = GetTabRect(e.Index);
            bool isselect = (e.Index == this.SelectedIndex);
            if (isselect) r.Height += 1;
            g.FillRectangle(new SolidBrush(isselect ? Color.White : page.BackColor), r); //改变选项卡标签的背景色 
            if (isselect) r.Height -= 1;
            g.DrawString(page.Text, page.Font, new SolidBrush(page.ForeColor), new PointF(r.X + this.Padding.Y, r.Y + this.Padding.Y));//PointF选项卡标题的位置 
            r.Offset(r.Width - iconWidth - 5, (r.Height - iconHeight) / 2);
            DrawClose(g, r, page.ForeColor);
        }
        //画关闭符号
        private void DrawClose(Graphics g, Rectangle r, Color forecolor)
        {
            using (Pen objpen = new Pen(forecolor))
            {
                //"\"线
                Point p1 = new Point(r.X, r.Y);
                Point p2 = new Point(p1.X + iconWidth, p1.Y + iconHeight);
                g.DrawLine(objpen, p1, p2);
                Point p3 = new Point(p1.X + 1, p1.Y);
                Point p4 = new Point(p2.X, p2.Y - 1);
                g.DrawLine(objpen, p3, p4);
                Point p5 = new Point(p1.X, p1.Y + 1);
                Point p6 = new Point(p2.X - 1, p2.Y);
                g.DrawLine(objpen, p5, p6);

                //"/"线
                Point p7 = new Point(p2.X, p1.Y);
                Point p8 = new Point(p1.X, p2.Y);
                g.DrawLine(objpen, p7, p8);
                Point p9 = new Point(p7.X - 1, p7.Y);
                Point p10 = new Point(p8.X, p8.Y - 1);
                g.DrawLine(objpen, p9, p10);
                Point p11 = new Point(p7.X, p7.Y + 1);
                Point p12 = new Point(p8.X + 1, p8.Y);
                g.DrawLine(objpen, p11, p12);
            }
        }
        /// <summary>
        /// OnMouseMove
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            Point p = e.Location;
            for (int i = 0; i < this.TabPages.Count; i++)
            {
                TabPage page = this.TabPages[i];
                Rectangle r = GetTabRect(i);
                r.Offset(r.Width - iconWidth - 5, (r.Height - iconHeight) / 2);
                r.Width = iconWidth;
                r.Height = iconHeight;
                Rectangle r2 = new Rectangle(r.X - 3, r.Y - 3, r.Width + 6, r.Height + 6);
                bool isselect = (i == this.SelectedIndex);
                Color clr = (isselect ? Color.White : page.BackColor);
                if (r2.Contains(p)) clr = Color.DarkSalmon;
                this.CreateGraphics().FillRectangle(new SolidBrush(clr), r2); //改变选项卡标签的背景色
                DrawClose(this.CreateGraphics(), r, page.ForeColor);
            }
        }
        /// <summary>
        /// OnMouseLeave
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeave(EventArgs e)
        {
            for (int i = 0; i < this.TabPages.Count; i++)
            {
                TabPage page = this.TabPages[i];
                Rectangle r = GetTabRect(i);
                r.Offset(r.Width - iconWidth - 5, (r.Height - iconHeight) / 2);
                r.Width = iconWidth;
                r.Height = iconHeight;
                Rectangle r2 = new Rectangle(r.X - 3, r.Y - 3, r.Width + 6, r.Height + 6);
                bool isselect = (i == this.SelectedIndex);
                Color clr = (isselect ? Color.White : page.BackColor);
                this.CreateGraphics().FillRectangle(new SolidBrush(clr), r2); //改变选项卡标签的背景色
                DrawClose(this.CreateGraphics(), r, page.ForeColor);
            }
        }
        /// <summary>
        /// OnMouseClick
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseClick(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)//左键
            {
                for (int i = 0; i < this.TabPages.Count; i++)
                {
                    TabPage page = this.TabPages[i];
                    Point p = e.Location;
                    Rectangle r = GetTabRect(i);
                    r.Offset(r.Width - iconWidth - 8, (r.Height - iconHeight) / 2 - 3);
                    r.Width = iconWidth + 6;
                    r.Height = iconHeight + 6;
                    if (r.Contains(p)) //点击特定区域时才发生 
                    {
                        PageClosed(page, new EventArgs());
                        break;
                    }
                }
            }
            else if (e.Button == MouseButtons.Right)//右键
            {
                for (int i = 0; i < this.TabPages.Count; i++)
                {
                    TabPage page = this.TabPages[i];
                    if (this.GetTabRect(i).Contains(new Point(e.X, e.Y)))
                    {
                        this.SelectedTab = page;
                        break;
                    }
                }
            }
        }
    }
}
